#include "StdAfx.h"
//-------------------------------------------------------------------------------------------------
//ProcParamDefInt.cpp  - Integer Parameter Definition for Image Processing Steps       
//-------------------------------------------------------------------------------------------------

#include "TinyXmlLib.h"
#include "TinyXmlHelper.h"
#include "ImageExptLib.h"
#include "ImageProcStepLibAuto.h"
#include "ProcParamDefInt.h"
#include "ProcParamInt.h"


ProcParamDefInt::ProcParamDefInt(void) : 
	_MinVal(0),
	_MaxVal(0)
{
}

ProcParamDefInt::ProcParamDefInt(int index, const char *Name, const char *Description, IPluginICProcStepsParamDef::Direction Dir, int MinVal, int MaxVal) :
	ProcParamDef(index, Name, Description, Dir),
	_MinVal(MinVal),
	_MaxVal(MaxVal)
{
	Validate();
}

ProcParamDefInt::~ProcParamDefInt(void)
{
}

long MXSTDMETHOD ProcParamDefInt::DestroyPtr(void)
{
	long cnt = InterlockedDecrement(&_refCnt);
	if ( cnt == 0 )
		delete this;
	return cnt;
}

long MXSTDMETHOD ProcParamDefInt::DuplicatePtr(void)
{
	return InterlockedIncrement(&_refCnt);
}

void *MXSTDMETHOD ProcParamDefInt::Dynamic_cast(const char *IId)
{
	void *rc = '\0';

	if (IId != '\0')
	{
		if (strncmp (IPluginICProcStepsParamDefIntIID::GUID(), IId, IMxPluginBaseIID::IIdSize) == 0)
			rc = static_cast<IPluginICProcStepsParamDefInt *>(this);
		else if (strncmp (IMxPluginBaseIID::GUID(), IId, IMxPluginBaseIID::IIdSize) == 0)
			rc = static_cast<IMxPluginBase *>(this);
		else
			rc = '\0';

		if (rc != '\0')
			((IMxPluginBase *)rc)->DuplicatePtr();
	}
	return rc;
}

bool ProcParamDefInt::IsMatch(IPluginICProcStepsParam  *param, IPluginICProcStepsParamDef::Direction dir)
{
	bool rc = false;

	if ( ProcParamDef::IsMatch(param, dir) == true)		//checks param is not null, param->IsValid, matchingParam->IsValid(), index, name, direction - logs errors
	{
		ProcParamInt *paramInt = dynamic_cast<ProcParamInt *>(param);
		if (paramInt == '\0')
			ImageExptLib::LogMsg("ProcParamDefInt::IsMatch()", "dynamic_cast<ProcParamInt *>(%s) is null", (param != '\0') ? param->GetName() : "param is null");
		else
		{
			std::string typeName(paramInt->GetType());
			if (typeName != IPluginICProcStepsParamDefIntP)
				ImageExptLib::LogMsg("ProcParamDefInt::IsMatch()", "%s->GetType() != IPluginICProcStepsParamDefIntP", param->GetName());
			else
			{
				if (paramInt->GetValue() < _MinVal) 
					ImageExptLib::LogMsg("ProcParamDefInt::IsMatch()", "%s->GetValue() is %d which is less than _MinVal %d", param->GetName(), paramInt->GetValue(), _MinVal);					
				else
				{
					if (paramInt->GetValue() > _MaxVal)
						ImageExptLib::LogMsg("ProcParamDefInt::IsMatch()", "%s->GetValue() is %d which is more than _MaxVal %d", param->GetName(), paramInt->GetValue(), _MaxVal);
					else
						rc = true;
				}
			}
		}
	}
	return rc;
}


const char *ProcParamDefInt::GetDefaultValueStr()
{
	if (IsValid() == false)
		ImageExptLib::LogMsg("ProcParamDefInt::GetDefaultValueStr", "ERROR: %s param %s is invalid", (ProcParamDef::GetDirection() == IPluginICProcStepsParamDef::input) ? "in" : "out", ProcParamDef::GetName());
	else
	{
		if (_DefaultValue == IPluginICProcStepsParamDefBadDefaultValue)
		{
			char buffer[20+1];
			int defaultVal = ((_MaxVal - _MinVal) / 2) + _MinVal;
			_DefaultValue = (_itoa_s(defaultVal, buffer, 20, 10) == 0) ? buffer : IPluginICProcStepsParamDefInvalidDefaultValue;
		}
	}
	return _DefaultValue.c_str();
}

const char *ProcParamDefInt::GetString()
{
	if (IsValid() == false)
		ImageExptLib::LogMsg("ProcParamDefInt::GetString", "ERROR: %s param %s is invalid", (ProcParamDef::GetDirection() == IPluginICProcStepsParamDef::input) ? "in" : "out", ProcParamDef::GetName());
	else
	{
		if (_Definition == IPluginICProcStepsParamDefInvalidDefinition)
		{
			char buffer[20+1];

			_Definition = (ProcParamDef::GetDirection() == IPluginICProcStepsParamDef::input) ? "[in]  " : "[out] ";
			_Definition += ProcParamDef::GetName();
			_Definition += " (";
			_Definition += GetTypeStr();
			_Definition += ") min=";

			_Definition += (_itoa_s(_MinVal, buffer, 20, 10) == 0) ? buffer : IPluginICProcStepsParamDefInvalidValue;
			_Definition += " max=";

			_Definition += (_itoa_s(_MaxVal, buffer, 20, 10) == 0) ? buffer : IPluginICProcStepsParamDefInvalidValue;
			_Definition +=  " - ";
			_Definition += ProcParamDef::GetDescription();
		}
	}
	return _Definition.c_str();
}

bool ProcParamDefInt::SetXml(void *xmlParam)
{
	bool rc = false;

	TiXmlElement *param = static_cast<TiXmlElement *>(xmlParam);
	if (param == '\0')
		ImageExptLib::LogMsg("ProcParamDefInt::SetXml", "param is null");
	else
	{
		if (ProcParamDef::SetXml(xmlParam) == true)	//base class checks IsValid()
		{
			rc = true;
		}
	}
	return rc;
}

bool	ProcParamDefInt::Validate()
{
	bool rc = false;

	if(ProcParamDef::Validate())
	{
		rc = true;
	}
	return rc;
}



//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------